using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectThongTinCongTy(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_THONGTINCONGTY,
				CurrentPage = page
			};
			return View(CreateViewModel(data));
		}

		public ActionResult DeleteThongTinCongTy(int id)
		{
			var deleteItem = _repThongTinCongTy.SelectByID(id);
			var fileName = deleteItem.Logo;
			var result = _repThongTinCongTy.Delete(id);
            if (result)
            {
                if (fileName != "noimages.jpg")
                {
                    var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                    DeleteFile(fullPath);
                }
            }
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_THONGTINCONGTY
            };
            return View("SelectThongTinCongTy", CreateViewModel(data));
		}

		public ActionResult DetailOfThongTinCongTy(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_THONGTINCONGTY,
				EditID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateThongTinCongTy(FormCollection forms)
		{
			string id = forms.GetValues("ThongTinCongTy_ID")[0];
			string tencongty = forms.GetValues("ThongTinCongTy_TenCongTy")[0];
			string trusochinh = forms.GetValues("ThongTinCongTy_TruSoChinh")[0];
			string dienthoai = forms.GetValues("ThongTinCongTy_DienThoai")[0];
			string fax = forms.GetValues("ThongTinCongTy_Fax")[0];
			string email = forms.GetValues("ThongTinCongTy_Email")[0];
			string lienhe = forms.GetValues("ThongTinCongTy_LienHe")[0];
			string phone = forms.GetValues("ThongTinCongTy_Phone")[0];
			string gioithieu = forms.GetValues("ThongTinCongTy_GioiThieu")[0];
			var file = Request.Files["ThongTinCongTy_Logo"];
			string refName = "";
			var editItem = _repThongTinCongTy.SelectByID(int.Parse(id));
			editItem.TenCongTy = tencongty;
			editItem.TruSoChinh = trusochinh;
			editItem.DienThoai = dienthoai;
			editItem.Fax = fax;
			editItem.Email = email;
			editItem.LienHe = lienhe;
			editItem.Phone = phone;
			editItem.GioiThieu = gioithieu;
			var result = false;
			if (file.ContentLength != 0)
			{
				result = UploadFile(file, "/Content/Images/Items/", id, ref refName);
				editItem.Logo = refName;
			}
			result = _repThongTinCongTy.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_THONGTINCONGTY,
				EditID = int.Parse(id),
				Updated = result,
			};
			return View("DetailOfThongTinCongTy", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertThongTinCongTy(FormCollection forms)
		{
			string tencongty = forms.GetValues("ThongTinCongTy_TenCongTy")[0];
			string trusochinh = forms.GetValues("ThongTinCongTy_TruSoChinh")[0];
			string dienthoai = forms.GetValues("ThongTinCongTy_DienThoai")[0];
			string fax = forms.GetValues("ThongTinCongTy_Fax")[0];
			string email = forms.GetValues("ThongTinCongTy_Email")[0];
			string lienhe = forms.GetValues("ThongTinCongTy_LienHe")[0];
			string phone = forms.GetValues("ThongTinCongTy_Phone")[0];
			string gioithieu = forms.GetValues("ThongTinCongTy_GioiThieu")[0];
			var file = Request.Files["ThongTinCongTy_Logo"];
			string refName = "";
			var newItem = new ThongTinCongTy
			{
				TenCongTy = tencongty,
				TruSoChinh = trusochinh,
				DienThoai = dienthoai,
				Fax = fax,
				Email = email,
				LienHe = lienhe,
				Phone = phone,
				GioiThieu = gioithieu,
				Logo = "noimages.jpg"
			};
			var result = _repThongTinCongTy.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
					result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
					if (result)
					{
						var addedItem = _repThongTinCongTy.SelectByID(newItem.ID);
						addedItem.Logo = refName;
						_repThongTinCongTy.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_THONGTINCONGTY,
				Added = result,
			};
			return View("SelectThongTinCongTy", CreateViewModel(data));
		}


	}
}